package com.xxrl.shop.controller;

import com.xxrl.shop.common.entity.JsonResult;
import com.xxrl.shop.common.utils.ResultTool;
import com.xxrl.shop.service.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.util.Pair;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author mis
 */
@Api(tags = {"upload", "image"})
@RequestMapping("/upload")
@RestController
public class FileController {

    private final FileService fileService;

    public FileController(FileService fileService) {
        this.fileService = fileService;
    }

    @ApiOperation(value = "Upload a new image, return url of uploaded image.", response = String.class)
    @PostMapping("img")
    public JsonResult<String> uploadImg(MultipartFile file) {
        Pair<String, Boolean> res = fileService.saveImg(file);
        if (res.getSecond()) {
            return ResultTool.success(res.getFirst());
        }
        return ResultTool.fail(res.getFirst());
    }
}
